package com.xuhai.blog19.common.exception;

import com.xuhai.blog19.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @Author PangJunjie
 * @Date 2023/2/6/006
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RTException.class)
    public R handlerRTException(RTException e) {
        return R.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException() {
        return R.error("数据库已存在该记录");
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoHandleFoundException() {
        return R.error(404, "路径地址不存在");
    }

    @ExceptionHandler(AuthenticationException.class)
    public R handlerAuthenticationException() {
        return R.error(401, "token失效");
    }

    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e) {
        e.printStackTrace();
        return R.error(e.getMessage());
    }
}
